<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理 - 权限管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #667eea;
            --secondary-color: #764ba2;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f8fafc;
        }

        .sidebar {
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
            color: white;
            height: 100vh;
            position: fixed;
            width: 280px;
            box-shadow: 5px 0 15px rgba(0,0,0,0.1);
            z-index: 1000;
        }

        .main-content {
            margin-left: 280px;
            padding: 30px;
            min-height: 100vh;
        }

        .nav-link {
            color: rgba(255,255,255,0.9) !important;
            padding: 15px 25px;
            border-bottom: 1px solid rgba(255,255,255,0.1);
            transition: all 0.3s ease;
            font-weight: 500;
        }

        .nav-link:hover {
            background: rgba(255,255,255,0.15);
            padding-left: 30px;
            color: white !important;
        }

        .nav-link.active {
            background: rgba(255,255,255,0.2);
            border-left: 4px solid white;
            color: white !important;
        }

        .search-card, .table-card {
            background: white;
            border-radius: 15px;
            padding: 25px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.08);
            border: none;
            margin-bottom: 25px;
        }

        .table-card {
            padding: 0;
            overflow: hidden;
        }

        .table-header {
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
            color: white;
            padding: 20px 25px;
        }

        .table {
            margin: 0;
        }

        .table th {
            border-top: none;
            font-weight: 600;
            padding: 15px 20px;
            background: #f8f9fa;
        }

        .table td {
            padding: 15px 20px;
            vertical-align: middle;
        }

        .table tbody tr {
            transition: all 0.3s ease;
        }

        .table tbody tr:hover {
            background-color: #f8f9fa;
        }

        .role-badge {
            margin: 2px;
            font-size: 0.75rem;
            padding: 4px 8px;
        }

        .form-control, .form-select {
            border-radius: 10px;
            border: 2px solid #e9ecef;
            padding: 10px 15px;
            transition: all 0.3s ease;
        }

        .form-control:focus, .form-select:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25);
        }

        .btn-primary {
            background: linear-gradient(135deg, var(--primary-color) 0%, var(--secondary-color) 100%);
            border: none;
            border-radius: 10px;
            padding: 10px 20px;
            font-weight: 500;
        }

        .badge.bg-pink {
            background: linear-gradient(135deg, #ec4899 0%, #db2777 100%) !important;
        }

        .pagination {
            margin: 0;
            padding: 20px;
            background: #f8f9fa;
            border-top: 1px solid #e9ecef;
        }

        .page-size-selector {
            width: 120px;
        }

        .btn-group-sm .btn {
            padding: 0.25rem 0.5rem;
        }
    </style>
</head>
<body>
<!-- 侧边栏 -->
<div class="sidebar">
    <div class="p-4">
        <div class="d-flex align-items-center mb-3">
            <div style="width: 50px; height: 50px; border-radius: 12px; background: white; display: flex; align-items: center; justify-content: center;" class="me-3">
                <i class="fas fa-shield-alt text-primary"></i>
            </div>
            <div>
                <h5 class="mb-0">权限管理系统</h5>
                <small class="opacity-75">用户管理</small>
            </div>
        </div>
        <p class="mb-0 text-light opacity-75">
            <i class="fas fa-user me-2"></i><span th:text="${currentUser.name}">用户</span>
        </p>
    </div>
    <nav class="nav flex-column">
        <a class="nav-link" href="/dashboard">
            <i class="fas fa-home me-3"></i>系统首页
        </a>
        <a class="nav-link active" href="/user/management">
            <i class="fas fa-users me-3"></i>用户管理
        </a>
        <a class="nav-link" href="/role/management">
            <i class="fas fa-user-shield me-3"></i>权限管理
        </a>
        <a class="nav-link" href="/user/profile">
            <i class="fas fa-user-cog me-3"></i>个人信息
        </a>
        <a class="nav-link" href="/logout">
            <i class="fas fa-sign-out-alt me-3"></i>退出登录
        </a>
    </nav>
</div>

<!-- 主内容区 -->
<div class="main-content">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1">
                <i class="fas fa-users me-2 text-primary"></i>用户管理
            </h2>
            <p class="text-muted mb-0">管理系统用户信息和权限分配</p>
        </div>
        <button class="btn btn-primary" id="addUserBtn">
            <i class="fas fa-plus me-2"></i>新增用户
        </button>
    </div>

    <!-- 搜索区域 -->
    <div class="search-card">
        <h5 class="mb-4">
            <i class="fas fa-search me-2 text-primary"></i>用户搜索
        </h5>
        <form th:action="@{/user/management}" method="get">
            <div class="row g-3">
                <div class="col-md-4">
                    <label class="form-label fw-semibold">用户名</label>
                    <input type="text" class="form-control" name="username"
                           th:value="${searchUsername}" placeholder="输入用户名">
                </div>
                <div class="col-md-4">
                    <label class="form-label fw-semibold">姓名</label>
                    <input type="text" class="form-control" name="name"
                           th:value="${searchName}" placeholder="输入真实姓名">
                </div>
                <div class="col-md-4">
                    <label class="form-label fw-semibold">邮箱</label>
                    <input type="text" class="form-control" name="email"
                           th:value="${searchEmail}" placeholder="输入邮箱地址">
                </div>
                <div class="col-md-4">
                    <label class="form-label fw-semibold">手机号</label>
                    <input type="text" class="form-control" name="mobilephone"
                           th:value="${searchMobilephone}" placeholder="输入手机号码">
                </div>
                <div class="col-md-4">
                    <label class="form-label fw-semibold">性别</label>
                    <select class="form-select" name="sex">
                        <option value="">全部性别</option>
                        <option value="1" th:selected="${searchSex == 1}">男性</option>
                        <option value="2" th:selected="${searchSex == 2}">女性</option>
                    </select>
                </div>
                <div class="col-md-4">
                    <label class="form-label fw-semibold">状态</label>
                    <select class="form-select" name="status">
                        <option value="">全部状态</option>
                        <option value="0" th:selected="${searchStatus == 0}">正常</option>
                        <option value="1" th:selected="${searchStatus == 1}">禁用</option>
                    </select>
                </div>
                <div class="col-12">
                    <div class="d-flex gap-2">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-search me-2"></i>搜索用户
                        </button>
                        <a href="/user/management" class="btn btn-outline-secondary">
                            <i class="fas fa-refresh me-2"></i>重置条件
                        </a>
                        <div class="flex-grow-1"></div>
                        <span class="text-muted align-self-center">
                                共 <strong th:text="${totalCount}">0</strong> 条记录
                            </span>
                    </div>
                </div>
            </div>

            <!-- 隐藏的分页参数 -->
            <input type="hidden" name="page" th:value="${currentPage}">
            <input type="hidden" name="pageSize" th:value="${pageSize}">
        </form>
    </div>

    <!-- 用户表格 -->
    <div class="table-card">
        <div class="table-header d-flex justify-content-between align-items-center">
            <h5 class="mb-0">
                <i class="fas fa-list me-2"></i>用户列表
            </h5>
            <div class="d-flex align-items-center">
                <span class="text-light me-2">每页显示：</span>
                <select class="form-select form-select-sm page-size-selector" onchange="changePageSize(this.value)">
                    <option value="5" th:selected="${pageSize == 5}">5条</option>
                    <option value="10" th:selected="${pageSize == 10}">10条</option>
                    <option value="20" th:selected="${pageSize == 20}">20条</option>
                    <option value="50" th:selected="${pageSize == 50}">50条</option>
                </select>
            </div>
        </div>
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                <tr>
                    <th width="80">ID</th>
                    <th width="120">用户名</th>
                    <th width="100">姓名</th>
                    <th width="180">邮箱</th>
                    <th width="130">手机号</th>
                    <th width="80">性别</th>
                    <th width="80">状态</th>
                    <th>角色</th>
                    <th width="150">创建时间</th>
                    <th width="120">操作</th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="user : ${users}">
                    <td th:text="${user.id}">1</td>
                    <td>
                        <strong th:text="${user.loginname}">admin</strong>
                    </td>
                    <td th:text="${user.name}">管理员</td>
                    <td th:text="${user.email}">admin@example.com</td>
                    <td th:text="${user.mobilephone}">13800138000</td>
                    <td>
                        <span th:if="${user.sex == 1}" class="badge bg-primary">男</span>
                        <span th:if="${user.sex == 2}" class="badge bg-pink">女</span>
                    </td>
                    <td>
                        <span th:if="${user.status == 0}" class="badge bg-success">正常</span>
                        <span th:if="${user.status == 1}" class="badge bg-danger">禁用</span>
                    </td>
                    <td>
                        <div th:each="role : ${user.roles}">
                                    <span class="badge role-badge"
                                          th:class="${role.roleKey == 'super_admin'} ? 'bg-danger' :
                                                     (${role.roleKey == 'admin'} ? 'bg-warning' :
                                                     (${role.roleKey == 'manager'} ? 'bg-success' : 'bg-info'))"
                                          th:text="${role.name}">
                                        角色
                                    </span>
                        </div>
                    </td>
                    <td th:text="${#temporals.format(user.createTime, 'yyyy-MM-dd HH:mm')}">2024-01-01 10:00</td>
                    <td>
                        <div class="btn-group btn-group-sm">
                            <!-- 编辑按钮 -->
                            <a th:href="@{/user/edit/{id}(id=${user.id})}" class="btn btn-outline-primary"
                               data-bs-toggle="tooltip" title="编辑用户">
                                <i class="fas fa-edit"></i>
                            </a>

                            <!-- 重置密码按钮 -->
                            <button type="button" class="btn btn-outline-success reset-password-btn"
                                    data-bs-toggle="modal" data-bs-target="#resetPasswordModal"
                                    th:attr="data-userid=${user.id}, data-username=${user.name}"
                                    title="重置密码">
                                <i class="fas fa-key"></i>
                            </button>

                            <!-- 删除按钮 -->
                            <button type="button" class="btn btn-outline-danger delete-user-btn"
                                    data-bs-toggle="modal" data-bs-target="#deleteUserModal"
                                    th:attr="data-userid=${user.id}, data-username=${user.name}"
                                    title="删除用户">
                                <i class="fas fa-trash"></i>
                            </button>
                        </div>
                    </td>
                </tr>
                <tr th:if="${users == null or users.empty}">
                    <td colspan="10" class="text-center py-4 text-muted">
                        <i class="fas fa-inbox fa-2x mb-3 d-block"></i>
                        暂无用户数据
                    </td>
                </tr>
                </tbody>
            </table>
        </div>

        <!-- 分页 -->
        <div class="pagination d-flex justify-content-between align-items-center">
            <div class="text-muted">
                显示第 <span th:text="${(currentPage - 1) * pageSize + 1}">1</span> 到
                <span th:text="${currentPage * pageSize > totalCount ? totalCount : currentPage * pageSize}">0</span> 条，
                共 <span th:text="${totalCount}">0</span> 条记录
            </div>
            <nav>
                <ul class="pagination mb-0">
                    <!-- 上一页 -->
                    <li th:class="${currentPage == 1} ? 'page-item disabled' : 'page-item'">
                        <a class="page-link" th:href="@{/user/management(page=${currentPage - 1}, pageSize=${pageSize}, username=${searchUsername}, email=${searchEmail}, status=${searchStatus}, name=${searchName}, mobilephone=${searchMobilephone}, sex=${searchSex})}">上一页</a>
                    </li>

                    <!-- 页码 -->
                    <li th:each="pageNum : ${#numbers.sequence(1, totalPages)}"
                        th:class="${pageNum == currentPage} ? 'page-item active' : 'page-item'">
                        <a class="page-link"
                           th:href="@{/user/management(page=${pageNum}, pageSize=${pageSize}, username=${searchUsername}, email=${searchEmail}, status=${searchStatus}, name=${searchName}, mobilephone=${searchMobilephone}, sex=${searchSex})}"
                           th:text="${pageNum}">1</a>
                    </li>

                    <!-- 下一页 -->
                    <li th:class="${currentPage == totalPages} ? 'page-item disabled' : 'page-item'">
                        <a class="page-link" th:href="@{/user/management(page=${currentPage + 1}, pageSize=${pageSize}, username=${searchUsername}, email=${searchEmail}, status=${searchStatus}, name=${searchName}, mobilephone=${searchMobilephone}, sex=${searchSex})}">下一页</a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
</div>

<!-- 重置密码确认模态框 -->
<div class="modal fade" id="resetPasswordModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">
                    <i class="fas fa-key text-warning me-2"></i>重置密码确认
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>确定要重置用户 <strong id="resetUserName"></strong> 的密码吗？</p>
                <p class="text-muted small">重置后密码将变为：<code>123456</code></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="resetPasswordForm" method="post">
                    <button type="submit" class="btn btn-warning">
                        <i class="fas fa-key me-2"></i>确认重置
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- 删除用户确认模态框 -->
<div class="modal fade" id="deleteUserModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title text-danger">
                    <i class="fas fa-exclamation-triangle me-2"></i>删除用户确认
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="alert alert-warning">
                    <i class="fas fa-exclamation-circle me-2"></i>
                    <strong>警告：此操作不可撤销！</strong>
                </div>
                <p>确定要删除用户 <strong id="deleteUserName"></strong> 吗？</p>
                <p class="text-muted small">删除后该用户的所有数据将无法恢复。</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="deleteUserForm" method="post">
                    <button type="submit" class="btn btn-danger">
                        <i class="fas fa-trash me-2"></i>确认删除
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>

<!-- 成功消息提示 -->
<div th:if="${success}" class="toast-container position-fixed top-0 end-0 p-3">
    <div class="toast align-items-center text-white bg-success border-0" role="alert" aria-live="assertive" aria-atomic="true">
        <div class="d-flex">
            <div class="toast-body">
                <i class="fas fa-check-circle me-2"></i>
                <span th:text="${success}"></span>
            </div>
            <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // 初始化工具提示
    document.addEventListener('DOMContentLoaded', function() {
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[title]'))
        var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
            return new bootstrap.Tooltip(tooltipTriggerEl)
        });

        // 自动显示成功消息
        var successToast = document.querySelector('.toast');
        if (successToast) {
            var toast = new bootstrap.Toast(successToast);
            toast.show();
        }
    });

    // 重置密码模态框事件
    var resetPasswordModal = document.getElementById('resetPasswordModal')
    resetPasswordModal.addEventListener('show.bs.modal', function (event) {
        var button = event.relatedTarget
        var userId = button.getAttribute('data-userid')
        var userName = button.getAttribute('data-username')

        var userNameSpan = resetPasswordModal.querySelector('#resetUserName')
        var form = resetPasswordModal.querySelector('#resetPasswordForm')

        userNameSpan.textContent = userName
        form.action = '/user/reset-password/' + userId
    })

    // 删除用户模态框事件
    var deleteUserModal = document.getElementById('deleteUserModal')
    deleteUserModal.addEventListener('show.bs.modal', function (event) {
        var button = event.relatedTarget
        var userId = button.getAttribute('data-userid')
        var userName = button.getAttribute('data-username')

        var userNameSpan = deleteUserModal.querySelector('#deleteUserName')
        var form = deleteUserModal.querySelector('#deleteUserForm')

        userNameSpan.textContent = userName
        form.action = '/user/delete/' + userId
    })

    // 新增用户按钮事件
    document.getElementById('addUserBtn').addEventListener('click', function() {
        window.location.href = '/user/add';
    });

    // 修改每页显示条数
    function changePageSize(pageSize) {
        const url = new URL(window.location.href);
        url.searchParams.set('pageSize', pageSize);
        url.searchParams.set('page', '1'); // 重置到第一页
        window.location.href = url.toString();
    }
</script>
</body>
</html>